//
//  GJJAgreeView.m
//  GongJiJin
//
//  Created by 凯朱 on 2022/4/6.
//

#import "GJJAgreeView.h"
@implementation GJJAgreeView
-(void)awakeFromNib{
    [super awakeFromNib];
    self.backgroundColor = [KColor_1 colorWithAlphaComponent:0.5];
    _bgView.layer.cornerRadius = 10;
    _bgView.layer.masksToBounds = YES;
    [self addTextLabel];
}

- (IBAction)cancelAction:(id)sender {
    if (self.btnBlock) {
        self.btnBlock(0);
    }
}

- (IBAction)agreeAction:(id)sender {
    if (self.btnBlock) {
        self.btnBlock(1);
    }
}

/*
 添加富文本点击事件
 */
-(void)addTextLabel{
    NSString *textStr = @"欢迎使用本APP，我们非常重视您的个人信息和隐私保护，在您使用本产品服务之前，请您务必审慎阅读《隐私政策》和《用户协议》，并充分理解协议条款内容。我们将严格按照您的同意的各项条款使用您的个人信息，以便为您更好的提供服务。";
    __block NSMutableAttributedString *text = [[NSMutableAttributedString alloc] initWithString:textStr];
    text.color = KColor_1;
    
    NSRange range = [textStr rangeOfString:@"《用户协议》"];
    [text setTextHighlightRange:range color:KColor_B font: kFontBold(14) backgroundColor:KClearColor tapAction:^(UIView * _Nonnull containerView, NSAttributedString * _Nonnull clickText, NSRange range, CGRect rect) {
        if (self.btnBlock) {
            self.btnBlock(11);
        }
    }];
    
    NSRange range2 = [textStr rangeOfString:@"《隐私政策》"];
    [text setTextHighlightRange:range2 color:KColor_B font: kFontBold(14) backgroundColor:KColor_B tapAction:^(UIView * _Nonnull containerView, NSAttributedString * _Nonnull clickText, NSRange range, CGRect rect) {
        if (self.btnBlock) {
            self.btnBlock(22);
        }
    }];
    
    YYLabel *textLabel = [YYLabel new];
    text.font = kFont(15);
    text.lineSpacing = 6;
    textLabel.numberOfLines = 0;
    textLabel.attributedText = text;
    //设置换行方式
    NSMutableParagraphStyle  *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    paragraphStyle.lineBreakMode = NSLineBreakByCharWrapping;
    text.paragraphStyle = paragraphStyle;
    
    //计算文本尺寸
    CGSize maxSize = CGSizeMake(KScreenWidth-90, MAXFLOAT);
    YYTextLayout *layout = [YYTextLayout layoutWithContainerSize:maxSize text:text];
    textLabel.textLayout = layout;
    CGFloat introHeight = layout.textBoundingSize.height;
    textLabel.left = 0;
    textLabel.width = maxSize.width;
    textLabel.height = introHeight;
    
    [_textBgView addSubview:textLabel];
}

@end
